package com.lemobar.pay.service.impl;

import com.lemobar.pay.config.AliPayConfig;
import com.lemobar.pay.config.EPayConfig;
import com.lemobar.pay.config.SignType;
import com.lemobar.pay.config.WxPayConfig;
import com.lemobar.pay.enums.LmbPayExResultEnum;
import com.lemobar.pay.enums.LmbPayPlatformEnum;
import com.lemobar.pay.enums.LmbPayTypeEnum;
import com.lemobar.pay.exception.LmbPayException;
import com.lemobar.pay.model.*;
import com.lemobar.pay.model.notify.PayOrderNotifyResult;
import com.lemobar.pay.service.LmbPayService;
import com.lemobar.pay.service.impl.alipay.AliPayServiceImpl;
import com.lemobar.pay.service.impl.alipay.AlipayBarCodeServiceImpl;
import com.lemobar.pay.service.impl.epay.EPayServiceImpl;
import com.lemobar.pay.service.impl.wx.WxPayMicroServiceImpl;
import com.lemobar.pay.service.impl.wx.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class LmbPayServiceImpl implements LmbPayService {

    private WxPayConfig wxPayConfig;
    private AliPayConfig aliPayConfig;
    private EPayConfig ePayConfig;

    /**
     * 微信付款码 正则格式
     */
    private final String wxAuthCodeRE = "1[0-5]\\d{16}";
    /**
     * 支付宝付款码 正则格式
     */
    private final String aliAuthCodeRE = "((2(5|6|7|8|9))|(30))\\d{14,22}";

    public void setWxPayConfig(WxPayConfig wxPayConfig) {
        this.wxPayConfig = wxPayConfig;
    }

    public void setAliPayConfig(AliPayConfig aliPayConfig) {
        this.aliPayConfig = aliPayConfig;
    }

    public void setEPayConfig(EPayConfig ePayConfig) {
        this.ePayConfig = ePayConfig;
    }

    @Override
    public PayResponse pay(PayRequest request) {
        Objects.requireNonNull(request, "request params must not be null");
        if (LmbPayPlatformEnum.WXPAY == request.getPayTypeEnum().getPlatform()) { //微信支付
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(wxPayConfig);
            return wxPayService.pay(request);
        } else if (LmbPayPlatformEnum.ALIPAY == request.getPayTypeEnum().getPlatform()) { // 支付宝支付
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(aliPayConfig);
            return aliPayService.pay(request);
        } else if (LmbPayPlatformEnum.EPAY == request.getPayTypeEnum().getPlatform()) { // 双乾支付
            EPayServiceImpl ePayService = new EPayServiceImpl();
            ePayService.setEPayConfig(ePayConfig);
            return ePayService.pay(request);
        }
        throw new RuntimeException("错误的支付方式");

    }

    /**
     * 付款码支付
     * 根据付款码格式 自动分发 微信付款 还是 支付宝付款
     * 微信：用户付款码条形码规则：18位纯数字，以10、11、12、13、14、15开头
     * 支付宝：用户付款码，25-30 开头的长度为 16-24 位的数字，实际字符串长度以开发者获取的付款码长度为准。
     */
    @Override
    public PayResponse payMicro(PayRequest request) {
        String authCode = request.getAuthCode();

        if (authCode.matches(wxAuthCodeRE)) { //微信支付
            WxPayServiceImpl wxPayService = new WxPayMicroServiceImpl();        // 微信-付款码支付
            wxPayService.setWxPayConfig(wxPayConfig);
            PayResponse response = wxPayService.pay(request);
            response.setPayTypeEnum(LmbPayTypeEnum.WXPAY_MICRO);
            return response;
        } else if (authCode.matches(aliAuthCodeRE)) { // 支付宝支付
            AliPayServiceImpl aliPayService = new AlipayBarCodeServiceImpl();   // 支付宝-付款码支付
            aliPayService.setAliPayConfig(aliPayConfig);
            PayResponse response = aliPayService.pay(request);
            response.setPayTypeEnum(LmbPayTypeEnum.ALIPAY_BARCODE);
            return response;
        }
        throw new RuntimeException("错误的付款码");

    }

    /**
     * 查询订单
     */
    @Override
    public QueryResponse query(QueryRequest request) {
        if (LmbPayPlatformEnum.WXPAY == request.getPayTypeEnum().getPlatform()) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.query(request);
        } else if (LmbPayPlatformEnum.ALIPAY == request.getPayTypeEnum().getPlatform()) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.query(request);
        } else if (LmbPayPlatformEnum.EPAY == request.getPayTypeEnum().getPlatform()) {
            EPayServiceImpl ePayService = new EPayServiceImpl();
            ePayService.setEPayConfig(this.ePayConfig);
            return ePayService.query(request);
        }
        throw new RuntimeException("错误的支付平台");
    }

    @Override
    public CloseResponse close(CloseRequest request) {
        if (request.getPayTypeEnum().getPlatform() == LmbPayPlatformEnum.WXPAY) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.close(request);
        } else if (request.getPayTypeEnum().getPlatform() == LmbPayPlatformEnum.ALIPAY) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.close(request);
        }
        throw new RuntimeException("尚未支持该种支付方式");
    }

    @Override
    public RefundResponse refund(RefundRequest request) {
        if (LmbPayPlatformEnum.WXPAY == request.getPayTypeEnum().getPlatform()) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.refund(request);
        } else if (LmbPayPlatformEnum.ALIPAY == request.getPayTypeEnum().getPlatform()) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.refund(request);
        } else if (LmbPayPlatformEnum.EPAY == request.getPayTypeEnum().getPlatform()) {
            EPayServiceImpl ePayService = new EPayServiceImpl();
            ePayService.setEPayConfig(this.ePayConfig);
            return ePayService.refund(request);
        }
        throw new RuntimeException("错误的支付平台");
    }

    @Override
    public RefundQueryResponse refundQuery(RefundQueryRequest request) {
        if (LmbPayPlatformEnum.WXPAY == request.getPayTypeEnum().getPlatform()) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.refundQuery(request);
        } else if (LmbPayPlatformEnum.ALIPAY == request.getPayTypeEnum().getPlatform()) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.refundQuery(request);
        }
        throw new RuntimeException("错误的支付平台");
    }

    @Override
    public ReverseResponse reverse(ReverseRequest request) {
        if (LmbPayPlatformEnum.WXPAY == request.getPayTypeEnum().getPlatform()) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.reverse(request);
        } else if (LmbPayPlatformEnum.ALIPAY == request.getPayTypeEnum().getPlatform()) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.reverse(request);
        }
        throw new RuntimeException("错误的支付平台");
    }

    /**
     * 同步返回
     */
    @Override
    public PayResponse syncNotify(HttpServletRequest request) {
        return null;
    }

    @Override
    public boolean verify(Map<String, String> toBeVerifiedParamMap, SignType signType, String sign) {
        return false;
    }

    /**
     * 异步回调
     */
    @Override
    public NotifyResponse asyncNotify(String notifyData) {
        //<xml>开头的是微信通知
        if (notifyData.startsWith("<xml>")) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.asyncNotify(notifyData);
        } else {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(aliPayConfig);
            return aliPayService.asyncNotify(notifyData);
        }
    }


    @Override
    public String downloadBill(DownloadBillRequest request) {

        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setWxPayConfig(this.wxPayConfig);

        return wxPayService.downloadBill(request);
    }

    @Override
    public String getQrCodeUrl(String productId) {
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        wxPayService.setWxPayConfig(this.wxPayConfig);

        return wxPayService.getQrCodeUrl(productId);
    }


    @Override
    public PayBankResponse payBank(PayBankRequest request) {
        if (request.getPayTypeEnum().getPlatform() == LmbPayPlatformEnum.WXPAY) {
            WxPayServiceImpl wxPayService = new WxPayServiceImpl();
            wxPayService.setWxPayConfig(this.wxPayConfig);
            return wxPayService.payBank(request);
        } else if (request.getPayTypeEnum().getPlatform() == LmbPayPlatformEnum.ALIPAY) {
            AliPayServiceImpl aliPayService = new AliPayServiceImpl();
            aliPayService.setAliPayConfig(this.aliPayConfig);
            return aliPayService.payBank(request);
        }
        throw new RuntimeException("尚未支持该种支付方式");
    }

    @Override
    public PayOrderNotifyResult parseOrderNotifyResult(String xmlData) {
        try {
            log.debug("通知请求参数：{}", xmlData);
            PayOrderNotifyResult result = PayOrderNotifyResult.fromXML(xmlData);
            log.debug("通知请求解析后的对象：{}", result);
            return result;
        } catch (Exception var5) {
            var5.printStackTrace();
            throw new LmbPayException(LmbPayExResultEnum.XML_PARAM_ERROR);
        }
    }
}